/*
 * @Author: fanzhiwei
 * @Date: 2024-08-16 09:20:29
 * @LastEditors: fanzhiwei
 * @LastEditTime: 2024-09-04 10:48:01
 * @FilePath: /common-web-ui/src/plugins/GlobalMethod.js
 * @Description: 常用全局方法挂载
 */
import Vue from "vue";
import store from "@/store";
import router from "@/router";
import { getUserMemoryInfo, uptUserMemoryInfo } from "@/api/common";
import "@/utils/c-tip";
import {
  addDateRange,
  clog,
  copyToClipboard,
  dictToFormatters,
  extend,
  getRules,
  resetForm,
  selectDictLabel,
} from "@/utils/common";

import { getDicts, getMultipleDicts } from "@/api/system/dict/data";
import { exportExcel, exportTmpl } from "@/utils/download";
import VueClipboard from "vue-clipboard2";
import jquery from "jquery";
window.$ = jquery;
/**
 * A simple vuejs 2 binding for clipboard.js
 * eg：let container = this.$refs.container
 * this.$copyText("Text to copy", container
 * */
Vue.use(VueClipboard);

Vue.prototype.getUserMemoryInfo = getUserMemoryInfo;
Vue.prototype.uptUserMemoryInfo = uptUserMemoryInfo;
Vue.prototype.getDicts = getDicts;
Vue.prototype.getMultipleDicts = getMultipleDicts;
Vue.prototype.selectDictLabel = selectDictLabel;
Vue.prototype.dictToFormatters = dictToFormatters;
Vue.prototype.getRules = getRules;
Vue.prototype.resetForm = resetForm;
Vue.prototype.addDateRange = addDateRange;
Vue.prototype.extend = extend;
Vue.prototype.exportExcel = exportExcel;
Vue.prototype.exportTmpl = exportTmpl;
Vue.prototype.clog = clog;
Vue.prototype.copyToClipboard = copyToClipboard;
/**
 * 生成uuid
 */
Vue.prototype.UUID = (len = 16) => {
  const crypto = window.crypto || window.msCrypto;
  const array = new Uint32Array(1);
  const r = crypto.getRandomValues(array)[0];
  const str = Date.now() + "" + r;
  return str.substring(str.length - len);
};
// 跳转并刷新
Vue.prototype.$linkto = (path, query = {}, refresh = true) => {
  const target = store.state.tagsView.visitedViews.find(
    (item) => item.path == path
  );
  if (target && refresh) {
    store.dispatch("tagsView/delCachedView", target).then(() => {
      Vue.prototype.$nextTick(() => {
        router.replace({
          path: path,
          query,
        });
      });
    });
  } else {
    router.push({ path, query });
  }
};
Vue.prototype.$close = (toPath = "", query = {}) => {
  const curPath = router.currentRoute.path;
  const target = store.state.tagsView.visitedViews.find(
    (item) => item.path == curPath
  );
  if (target) {
    store.dispatch("tagsView/delView", target).then(({ visitedViews }) => {
      if (toPath) {
        Vue.prototype.$linkto(toPath, query);
      } else {
        const latestView = visitedViews.slice(-1)[0];
        if (latestView) {
          router.push(latestView);
        } else {
          if (target.name === "Dashboard") {
            router.replace({ path: "/redirect" + target.fullPath });
          } else {
            router.push("/");
          }
        }
      }
    });
  }
};

Vue.prototype.$getStaticUrl = (path) => {
  return new URL(path, import.meta.url).href;
};
